package com.kly.order.serviceImpl;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.dto.DiscountListDto;
import com.kly.dto.DiscountListResp;
import com.kly.dto.ProductDiscountConfigDto;
import com.kly.order.db.dao.ProductDiscountConfigDao;
import com.kly.order.db.entity.ProductDiscountConfigEntity;
import com.kly.req.DiscountAddReq;
import com.kly.service.ProductDiscountConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;


@Service("productDiscountConfigService")
public class ProductDiscountConfigServiceImpl implements ProductDiscountConfigService {


    @Resource
    private ProductDiscountConfigDao productDiscountConfigDao;

    @Override
    public ProductDiscountConfigDto productDiscountCountByPromotionType(Integer promotionType, String appCode, Integer memberType) {
        ProductDiscountConfigEntity productDiscountConfigEntity = productDiscountConfigDao.productDiscountCountByPromotionType(promotionType, appCode, memberType);
        if (productDiscountConfigEntity != null) {
            return toDto(productDiscountConfigEntity);
        }
        return null;
    }

    @Override
    public List<ProductDiscountConfigDto> productDiscountByPromotionType(Integer promotionType, String appCode) {
        List<ProductDiscountConfigEntity> productDiscountConfigEntityList = productDiscountConfigDao.productDiscountByPromotionType(promotionType, appCode);
        if (!productDiscountConfigEntityList.isEmpty()) {
            List<ProductDiscountConfigDto> productDiscountConfigDtos = new ArrayList<>();
            for (ProductDiscountConfigEntity productDiscountConfigEntity : productDiscountConfigEntityList) {
                productDiscountConfigDtos.add(toDto(productDiscountConfigEntity));
            }
            return productDiscountConfigDtos;
        }
        return null;
    }

    private ProductDiscountConfigDto toDto(ProductDiscountConfigEntity productDiscountConfigEntity) {
        ProductDiscountConfigDto productDiscountConfigDto = new ProductDiscountConfigDto();
        productDiscountConfigDto.setPopTitle(productDiscountConfigEntity.getPopTitle());
        productDiscountConfigDto.setPopContent(productDiscountConfigEntity.getPopContent());
        productDiscountConfigDto.setPopOverTime(productDiscountConfigEntity.getPopOverTime());
        productDiscountConfigDto.setPopTag(productDiscountConfigEntity.getPopTag());
        productDiscountConfigDto.setPromotionPrice(productDiscountConfigEntity.getPromotionPrice());
        productDiscountConfigDto.setPromotionDelPrice(productDiscountConfigEntity.getPromotionDelPrice());
        productDiscountConfigDto.setPromotionNote(productDiscountConfigEntity.getPromotionNote());
        productDiscountConfigDto.setPopButton(productDiscountConfigEntity.getPopButton());
        productDiscountConfigDto.setPopButtonTitle(productDiscountConfigEntity.getPopButtonTitle());
        productDiscountConfigDto.setPopButtonContent(productDiscountConfigEntity.getPopButtonContent());
        productDiscountConfigDto.setPromotionType(productDiscountConfigEntity.getPromotionType());
        productDiscountConfigDto.setMemberType(productDiscountConfigEntity.getMemberType());
        productDiscountConfigDto.setAppCode(productDiscountConfigEntity.getAppCode());
        productDiscountConfigDto.setProductCode(productDiscountConfigEntity.getProductCode());
        return productDiscountConfigDto;
    }


    @Override
    public DiscountListResp listDiscount(Integer promotionType, Integer memberType, String appCode, Page page) {
        final Integer discountCnt = productDiscountConfigDao.getDiscountCnt(promotionType, memberType, appCode);
        if (discountCnt == 0) {
            return DiscountListResp.builder().totalNum(0).build();
        }

        List<ProductDiscountConfigEntity> discountConfigEntities = productDiscountConfigDao.listDiscount(promotionType, memberType, appCode, page);

        final List<DiscountListDto> discountListDtos = discountConfigEntities.stream().map(o -> {
            final DiscountListDto dto = DiscountListDto.builder().build();
            BeanUtils.copyProperties(o, dto);
            if (o.getDeleted()) {
                dto.setEnableFlag(0);
            } else {
                dto.setEnableFlag(1);
            }
            dto.setId(Integer.valueOf(String.valueOf(o.getId())));
            return dto;
        }).collect(Collectors.toList());

        return DiscountListResp.builder().totalNum(discountCnt).discountListDtoList(discountListDtos).build();
    }

    @Override
    public DiscountListDto getDiscount(Integer id) {
        final ProductDiscountConfigEntity discountConfig = productDiscountConfigDao.getDiscountById(id);
        if (discountConfig == null) {
            return null;
        }

        final DiscountListDto dto = DiscountListDto.builder().build();
        BeanUtils.copyProperties(discountConfig, dto);
        if (discountConfig.getDeleted()) {
            dto.setEnableFlag(0);
        } else {
            dto.setEnableFlag(1);
        }
        dto.setId(id);

        return dto;
    }

    @Override
    public Response addDiscount(DiscountAddReq addReq, String appCode) {
        List<ProductDiscountConfigEntity> discountConfigEntities = productDiscountConfigDao.listDiscount(addReq.getPromotionType(), addReq.getMemberType(), appCode, null);
        if (CollectionUtils.isNotEmpty(discountConfigEntities)) {
            final boolean existsEnableRecord = discountConfigEntities.parallelStream().anyMatch(o -> !o.getDeleted());
            if (existsEnableRecord) {
                if (addReq.getEnableFlag() == 1) {
                    return Response.error("Add discount fail: the record has exists");
                }
            }
        }

        ProductDiscountConfigEntity entity = new ProductDiscountConfigEntity();
        BeanUtils.copyProperties(addReq, entity);
        if (addReq.getEnableFlag() == 1) {
            entity.setDeleted(Boolean.FALSE);
        } else {
            entity.setDeleted(Boolean.TRUE);
        }

        productDiscountConfigDao.addDiscount(entity);

        return Response.ok();
    }

    @Override
    public Response modifyDiscount(DiscountListDto addReq) {
        if (addReq.getId() == null) {
            return Response.error("Params:[id] should not be empty");
        }

        ProductDiscountConfigEntity entity = productDiscountConfigDao.getDiscountById(addReq.getId());

        List<ProductDiscountConfigEntity> discountConfigEntities = productDiscountConfigDao.listDiscount(addReq.getPromotionType(), addReq.getMemberType(), addReq.getAppCode(), null);
        if (CollectionUtils.isNotEmpty(discountConfigEntities)) {
            final boolean existsEnableRecord = discountConfigEntities.parallelStream().anyMatch(o -> !o.getDeleted() && !o.getId().equals(entity.getId()));
            if (existsEnableRecord) {
                if (addReq.getEnableFlag() == 1) {
                    return Response.error("Add discount fail: the record has exists");
                }
            }
        }


        BeanUtils.copyProperties(addReq, entity);
        if (addReq.getEnableFlag() == 1) {
            entity.setDeleted(Boolean.FALSE);
        } else {
            entity.setDeleted(Boolean.TRUE);
        }
        productDiscountConfigDao.updateByPrimaryKey(entity);
        return Response.ok();
    }

    @Override
    public Response removeDiscount(Integer id) {
        if (id == null) {
            return Response.error("Params:[ids] should not be empty");
        }

        final int removeNum = productDiscountConfigDao.removeDiscount(id);

        return Response.ok(removeNum);
    }
}
